//
//  WifiSettingViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/27.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "WifiSettingViewController.h"
#import "DeviceListViewController.h"
#import <Reachability/Reachability.h>
#import "DeviceViewModel.h"

@interface WifiSettingViewController ()
@property(nonatomic,weak)IBOutlet UIImageView *inputTextBg;
@property(nonatomic,weak)IBOutlet UIImageView *prdIcon;
@property(nonatomic,weak)IBOutlet UILabel *prdName;
@property(nonatomic,weak)IBOutlet UILabel *prdCode;
@property(nonatomic,weak)IBOutlet UIButton *nexBtn;
@property(nonatomic,strong)DeviceViewModel *deviceInfoViewModel;
@property(nonatomic,assign)BOOL canConnectToNetWork;
@end

@implementation WifiSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"配置wifi";
    [self configUI];
}
-(void)configUI{
    [self.inputTextBg setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    self.nexBtn.layer.cornerRadius = 20;
    self.prdName.text = self.deviceName;
    self.prdCode.text = self.deviceCode;
}
-(IBAction)searchBtnPressed:(id)sender{
    [SVProgressHUD showWithStatus:@"正在绑定设备"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([self internetStatus] == 0) {
            [SVProgressHUD dismiss];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"切换网络失败，是否需要手动切换Wi-Fi" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSURL *url1 = [NSURL URLWithString:@"App-prefs:root=WIFI"];
                if ([[UIApplication sharedApplication] canOpenURL:url1]){
                    [[UIApplication sharedApplication]openURL:url1 options:@{} completionHandler:nil];
                }
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alert animated:YES completion:nil];
            });
        }else{
            [self bindDevice];
        }
    });
}
#pragma mark - instance
-(DeviceViewModel*)deviceInfoViewModel{
    if (!_deviceInfoViewModel) {
        _deviceInfoViewModel = [[DeviceViewModel alloc]init];
    }
    return _deviceInfoViewModel;
}
-(void)bindDevice{
    if (EC_ISNullStr(self.deviceId)) {
        [SVProgressHUD dismiss];
        Toast(@"设备id缺失");
        return;
    }
    [self.deviceInfoViewModel fetchDeviceInfo:self.deviceId cmp:^(NSDictionary * _Nonnull deviceInfo) {
        if ([deviceInfo isKindOfClass:[NSDictionary class]]) {
            if ([[deviceInfo objectForKey:@"online"] boolValue]) {
                [self.deviceInfoViewModel bindDevice:self.deviceId cmp:^{
                    [SVProgressHUD dismiss];
                    Toast(@"绑定成功");
                    [self.rt_navigationController popToViewController:[self.rt_navigationController.rt_viewControllers objectAtIndex:1] animated:YES complete:nil];
                } fail:^{
                    [SVProgressHUD dismiss];
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"切换网络失败，是否需要手动切换Wi-Fi" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        NSURL *url1 = [NSURL URLWithString:@"App-prefs:root=WIFI"];
                        if ([[UIApplication sharedApplication] canOpenURL:url1]){
                            [[UIApplication sharedApplication]openURL:url1 options:@{} completionHandler:nil];
                        }
                    }]];
                    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:alert animated:YES completion:nil];
                    });
                }];
            }else{
                [SVProgressHUD dismiss];
                Toast(@"设备配网失败,请重新配网");
            }
        }else{
            [SVProgressHUD dismiss];
            Toast(@"没有扫描到设备");
            [self.rt_navigationController popToRootViewControllerAnimated:YES complete:nil];
        }
    }];
}

- (NSInteger)internetStatus {
    Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    return internetStatus;
}
/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
